package org.bitmap.mm.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;

/**
 * 单纯的下载集合List图片线程，图片如果本地存在了就不再重复下载了.
 * @author iWei
 */
class SimpleBitmapTasks extends Thread {

	private  List<String> mUrlList;
	private LoveBitmapManager mLoveBitmapManager;
	private OnAllFetchCompleteListener mAllCallback;
	private int mTaskId;
	public SimpleBitmapTasks(LoveBitmapManager bitmanager, List<String> urllist,int taskId,OnAllFetchCompleteListener callback) {
		mUrlList = new ArrayList<String>(urllist);
		mLoveBitmapManager = bitmanager;
		mAllCallback = callback;
		mTaskId = taskId;
	}
	/**
	 * 
	 * 静态方法，检查指定URL图片是否已经下载到本地
	 * @param ctx
	 * @param url
	 * @return
	 */
	public static final boolean hasBitmap(Context ctx,String url){
		final File cacheDir = FileLove.getExternalCacheDir(ctx);
		if (!cacheDir.exists()) {
			cacheDir.mkdir();
		}
		final File cachedFile = new File(cacheDir,
				FileLove.hashKeyForDisk(url));
		if (cachedFile.exists()) {
			return true;
		}else{
			return false;
		}
	}
	@Override
	public void run() {
		for(String imageUrl:mUrlList){
			File mFile = HttpLove.downloadBitmapToFile(
					mLoveBitmapManager.getContext(), imageUrl);
			if(mAllCallback!=null){
				mAllCallback.onFetchComplete(mTaskId,imageUrl,mFile);
			}
			System.gc();
		}
		if(mAllCallback!=null){
			mAllCallback.onExit(mTaskId);
		}
		System.gc();
	}
	public OnAllFetchCompleteListener getOnAllFetchCompleteListener() {
		return mAllCallback;
	}
	public void setOnAllFetchCompleteListener(OnAllFetchCompleteListener mAllCallback) {
		this.mAllCallback = mAllCallback;
	}

}
